package com.mes.service.yzl;


import com.mes.dao.yzl.OutlibMapper;
import com.mes.entity.hpf.SaleOutDetail;
import com.mes.entity.hpf.SalePlanDetail;
import com.mes.entity.yzl.Condition;
import com.mes.entity.yzl.CustomerOrder;
import com.mes.entity.yzl.OutLib;
import com.mes.entity.yzl.ProContactOrder;
import com.mes.entity.yzlExtend.ExtendOutlib;
import com.mes.entity.yzlExtend.ExtendProductor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OutlibServiceImpl implements OutlibService{

    @Resource
    OutlibMapper outlibMapper;

    @Override
    public int checkNumUnique(String returnNumber) {
        return outlibMapper.checkNumUnique(returnNumber);
    }

    @Override
    public long addOutOrder(OutLib outLib) {
        return outlibMapper.addOutOrder(outLib);
    }

    @Override
    public void addCentor(ProContactOrder proContactOrder) {
        outlibMapper.addCentor(proContactOrder);
        /*ProContactOrder pro = outlibMapper.findCusCenterProByPid(proContactOrder.getId());
        List<ExtendProductor> extendProductorList = outlibMapper.customerOrderXq(pro.getCustomerOrderId());
        int num = 0;
        for(int i=0;i<extendProductorList.size();i++){
        	if(extendProductorList.get(i).getAddOutIsSelect().equals("0")){
        		num = 1;
        	}
        }
        if(num==0){
        	outlibMapper.setSelectCusOrder(proContactOrder.getId(),0);
        }*/
    }

    @Override
    public int countOutlibCount(Condition condition) {
        return outlibMapper.countOutlibCount(condition);
    }

    @Override
    public List<ExtendOutlib> listOutlibList(Condition condition) {
        return outlibMapper.listOutlibList(condition);
    }

    @Override
    public List<ExtendProductor> outOrderXq(long id) {
        return outlibMapper.outOrderXq(id);
    }

    @Override
    public ExtendOutlib findOutorderById(long id) {
        return outlibMapper.findOutorderById(id);
    }

    @Override
    public long updateOutLib(OutLib outLib) {
        return outlibMapper.updateOutLib(outLib);
    }

    @Override
    public void updateCentorOutLib(ProContactOrder proContactOrder) {
        outlibMapper.updateCentorOutLib(proContactOrder);
    }

    @Override
    public List<OutLib> outLibList() {
        List<OutLib> newOutLibList = new ArrayList<OutLib>();
		
		List<OutLib> outlibList = outlibMapper.outLibList();
		
		for (int i = 0; i < outlibList.size(); i++) {
			OutLib outLib = outlibList.get(i);
			List<ExtendProductor> extendProductorList = outlibMapper.outLibXq(outLib.getId());
			outLib.setExtendProductorList(extendProductorList);
			newOutLibList.add(outLib);
		}
		return newOutLibList;
    }

    @Override
    public String outLibNumLast() {
        String returnNumber = "";
        String returnNumberDate = "";
        DateFormat format = new SimpleDateFormat("yyyyMMdd");
        returnNumberDate = format.format(new Date());
        returnNumber = "C"+returnNumberDate+"00001";

        for(int i=1;i<=99999;i++){
            int dedaoNumber = outlibMapper.checkNumUnique(returnNumber);
            if(dedaoNumber == 0){
                break;
            }else{
                switch (String.valueOf(i).length()){
                    case 1 : returnNumber = "C"+returnNumberDate+"0000"+i;break;
                    case 2 : returnNumber = "C"+returnNumberDate+"000"+i; break;
                    case 3 : returnNumber = "C"+returnNumberDate+"00"+i; break;
                    case 4 : returnNumber = "C"+returnNumberDate+"0"+i; break;
                    case 5 : returnNumber = "C"+returnNumberDate+i; break;
                    default : returnNumber = "C"+returnNumberDate+"1000"+i; break;
                }
                //returnNumber = returnNumber + "00" + i;//以后改进
            }
        }
        return returnNumber;
    }

	@Override
	public int deleteOutLibOrder(long id) {
		// TODO Auto-generated method stub
		return outlibMapper.deleteOutLibOrder(id);
	}

	@Override
	public int outlibApproveds(List<Integer> list) {
		// TODO Auto-generated method stub
		return outlibMapper.outlibApproveds(list);
	}

	@Override
	public int outlibUnApproveds(List<Integer> list) {
		// TODO Auto-generated method stub
		return outlibMapper.outlibUnApproveds(list);
	}

	@Override
	public void deleteLibXq(long outLibId) {
		// TODO Auto-generated method stub
		List<ExtendProductor> eList = outlibMapper.outOrderXq(outLibId);
    	for(int i=0;i<eList.size();i++){
    		outlibMapper.setSelectCusOrder(eList.get(i).getPid(), 1);
    	}
		outlibMapper.deleteLibXq(outLibId);
	}

	@Override
	public int libOutbound(List<Integer> list) {
		// TODO Auto-generated method stub
		return outlibMapper.libOutbound(list);
	}

	@Override
	public int cusOrderbound(List<Integer> list) {
		// TODO Auto-generated method stub
		return outlibMapper.cusOrderbound(list);
	}

	@Override
	public int cusOrderCount(Condition condition) {
		// TODO Auto-generated method stub
		return outlibMapper.planDetailCount(condition);
	}

	@Override
	public List<SalePlanDetail> cusOrderList(Condition condition) {
		// TODO Auto-generated method stub
		return outlibMapper.planDetailList(condition);
	}

	@Override
	public List<ExtendProductor> addCusOrderXq(long id) {
		// TODO Auto-generated method stub
		return outlibMapper.addCusOrderXq(id);
	}

	@Override
	public List<ExtendProductor> orderXq(long id) {
		// TODO Auto-generated method stub
		return outlibMapper.customerOrderXq(id);
	}

	@Override
	public ProContactOrder findCusCenterProByPid(long pid) {
		// TODO Auto-generated method stub
		return outlibMapper.findCusCenterProByPid(pid);
	}

	@Override
	public int cusOrderboundTwo(List<Integer> list) {
		// TODO Auto-generated method stub
		return outlibMapper.cusOrderboundTwo(list);
	}

	@Override
	public void addOutDetail(SaleOutDetail saleOutDetail) {
		// TODO Auto-generated method stub
		outlibMapper.addOutDetail(saleOutDetail);
	}

	@Override
	public void updatePlanDetailStatus(SalePlanDetail salePlanDetail) {
		// TODO Auto-generated method stub
		outlibMapper.updatePlanDetailStatus(salePlanDetail);
	}

	@Override
	public List<SaleOutDetail> outDetail(long id) {
		// TODO Auto-generated method stub
		return outlibMapper.outDetail(id);
	}

	@Override
	public void updateOutDetail(SaleOutDetail saleOutDetail) {
		// TODO Auto-generated method stub
		outlibMapper.updateOutDetail(saleOutDetail);
	}

	@Override
	public void deleteOutDetail(long id) {
		// TODO Auto-generated method stub
		List<SaleOutDetail> saleOutDetails = outlibMapper.outDetail(id);
		for(int i=0;i<saleOutDetails.size();i++){
			SaleOutDetail saleOutDetail = saleOutDetails.get(i);
			SalePlanDetail salePlanDetail = outlibMapper.findPlanDetailById(saleOutDetail.getPlanDetailID());
			salePlanDetail.setLastPlanQ(salePlanDetail.getLastPlanQ()+saleOutDetail.getOutQ());
			salePlanDetail.setAddOutIsSelect("0");
    		outlibMapper.setSelectPlanDetail(salePlanDetail);
    	}
		outlibMapper.deleteOutDetail(id);
	}

}
